<template>
  <view>

    <my-search @click="gotoSearch"  ></my-search>
    <view class="scroll-view-container">
      <scroll-view scroll-y="true" class="left-scrollview" :style="{height:this.wh+'px'}">
        <block v-for="(item,i) in cateList " :key="i">
          <view @click="cateActiveChange(i)" :class="['left-scrollview-item',i===cateActive?'active':'']">
            {{item.cat_name}}
          </view>
        </block>
      </scroll-view>
      <!-- 右边 -->
      <scroll-view scroll-y="true" class="right-scrollview" :scrollTop="scrollTop" :style="{height:wh+'px'}">
        <view class="cate-level2" v-for="(item,i) in cateListLevel2 " :key="i">
          <!-- 二级分类 -->
          <view class="cate-level2-titel">/{{item.cat_name}}/
            <!-- 三级分类 -->
            <view class="cate-level3-list">
              <view class="cate-level3-list-item" v-for=" (item2,i2) in item.children " :key="i2"
                @click="gotoGoodsDetail(item2)">
                <image :src="item2.cat_icon.replace('api-ugo-dev','api-hmugo-web')" mode=""></image>
                <text>{{item2.cat_name}}</text>
              </view>
            </view>
          </view>
        </view>
      </scroll-view>
    </view>


  </view>
</template>

<script>
  import tabbarNumber from "@/maxins/tabbar.js"
  export default {
      mixins:[tabbarNumber],
    data() {
      return {
        wd: 999,
        wh: 999,
        cateActive: 0,
        cateList: [],
        scrollTop: 0,
        //二级分类
        cateListLevel2: []
      }
    },
    onLoad() {
      const sysInfo = uni.getSystemInfoSync()
      this.wd = sysInfo.windowWidth
      this.wh = sysInfo.windowHeight-50

      this.getCateList()

    },
    methods: {
      async getCateList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/categories')
        if (res.meta.status !== 200) {
          return uni.$showMsg()
        }
        this.cateList = res.message
        this.cateListLevel2 = res.message[0].children
      },
      gotoGoodsDetail(item) {
        uni.navigateTo({
          url: '/subpackges/goods_detail/goods_detail?cid=' + item.cat_id
        })
      },
      gotoSearch(){
        uni.navigateTo({
          url:'/subpackges/search/search'
        })
      },
      cateActiveChange(i) {
        this.cateActive = i
        this.cateListLevel2 = this.cateList[i].children
        this.scrollTop = this.scrollTop == 0 ? 1 : 0
      },
    }
  }
</script>

<style lang="scss">
  .scroll-view-container {
    display: flex;

    .left-scrollview {
      width: 120px;
    }
  }

  .left-scrollview-item {
    background-color: #f7f7f7;
    line-height: 60px;
    font-size: 16px;
    text-align: center;

    &.active {
      background-color: #ffffff;
      position: relative;

      &::before {
        content: '';
        display: block;
        background-color: red;
        width: 3px;
        height: 30px;
        top: 50%;
        position: absolute;
        transform: translateY(-50%);
      }
    }
  }


  .cate-level2-titel {
    font-size: 18px;
    text-align: center;
    font-weight: 900;
    padding: 15px 0;
  }

  .cate-level3-list {
    display: flex;
    flex-wrap: wrap;

    .cate-level3-list-item {
      width: 33.3%;
      display: flex;
      font-size: 12px;
      font-weight: 400;
      justify-content: center;
      flex-direction: column;
      align-items: center;
      margin-bottom: 10px;

      image {
        height: 60px;
        width: 60px;
      }
    }
  }
</style>